package com.software.shipperagent.dao;

import java.security.NoSuchAlgorithmException;
import java.util.List;

public class ShipperAgentTransaction {

	private ShipperAgentDao shipperAgentDao;

	
	public ShipperAgentDao getShipperAgentDao() {
		return shipperAgentDao;
	}

	public void setShipperAgentDao(ShipperAgentDao shipperAgentDao) {
		this.shipperAgentDao = shipperAgentDao;
	}

	public boolean login(String email,String password){
		ShipperAgent shipperAgent = new ShipperAgent();
		shipperAgent.setEmail(email);
		shipperAgent.setPassword(password);
		ShipperAgent sa = shipperAgentDao.queryShipperAgent(shipperAgent);
		if(sa != null){
			if(password.equals(sa.getPassword())){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 
	 * 前提：判断账户是否已经被注册
	 * @param username
	 * @param password
	 * @param email
	 * @param description
	 * @param phone
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public boolean register(String name,String password, String email,String phone,String description){
		ShipperAgent shipperAgent = new ShipperAgent();
		shipperAgent.setEmail(email);
		shipperAgent.setPassword(password);
		shipperAgent.setPhone(phone);
		shipperAgent.setName(name);
		shipperAgent.setDescription(description);
		
		ShipperAgent sa = shipperAgentDao.queryShipperAgent(shipperAgent);
		if(sa != null){
			return false;
		}
		try{
			shipperAgentDao.addShipperAgent(shipperAgent);
			return true;
		}catch(Exception e){
			return false;
		}	
	}
	
	public List<ShipperAgent> getAllAgents(){
		return shipperAgentDao.queryAllShippingAgent();
	}
}
